﻿using System;
using System.Configuration;
using System.Web;
using Core.AdminUser;
using Core.Library;

namespace CMS
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //
            txtUsername.Focus();
            siteName.Text = ConfigurationManager.AppSettings["SiteName"];

            if (IsPostBack) return;
            var userCookies = Request.Cookies["UserName"];
            var counts = Request.Cookies["counter"];
            if (userCookies == null) return;
            if (counts != null)
                if (ConvertUtility.ConvertTo<int>(counts.Value) > 3)
                    btnLogin.Enabled = false;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (!IsPostBack)
                return;

            var cookieUser = Request.Cookies["UserName"];
            var loginCount = Request.Cookies["counter"];

            if (cookieUser != null && loginCount != null)
            {
                var count = ConvertUtility.ConvertTo<int>(loginCount.Value);
                if (count >= 3)
                {
                    ltMessage.Text = MessageUtility.ShowMessage(null, "Tài khoản bị khóa trong vòng 15 phút", MessageType.Failure);
                    btnLogin.Enabled = false;
                    Response.Cookies["UserName"].Expires = DateTime.Now.AddMinutes(60);
                    Response.Cookies["counter"].Expires = DateTime.Now.AddMinutes(60);
                }
                else
                {
                    ltMessage.Text = MessageUtility.ShowMessage(null, "Thông tin đăng nhập không chính xác", MessageType.Information);
                }
            }
        }

        private int _counter;
        protected void BtnLoginClicked(object sender, EventArgs e)
        {
            var returnUrl = Request.QueryString["returnUrl"];
            if (AdminUserController.Login(txtUsername.Text.Trim(), SecurityUtility.MD5Encrypt(txtPassword.Text.Trim()), chkRememberPwd.Checked))
            {
                Response.Redirect(returnUrl ?? ConfigurationManager.AppSettings["AdminPage"]);
            }
            else
            {
                var userCookies = Request.Cookies["UserName"];
                var counts = Request.Cookies["counter"];
                if (userCookies != null && counts != null)
                {
                    _counter = ConvertUtility.ConvertTo<int>(counts.Value);
                    _counter++;

                    var userCookie = new HttpCookie("UserName", txtUsername.Text);
                    var count = new HttpCookie("counter", _counter.ToString());
                    Response.Cookies.Set(userCookie);
                    Response.Cookies.Set(count);
                }
                else
                {
                    _counter++;
                    var userCookie = new HttpCookie("UserName", txtUsername.Text);
                    var count = new HttpCookie("counter", _counter.ToString());
                    Response.Cookies.Set(userCookie);
                    Response.Cookies.Set(count);
                }
            }
        }
    }
}